from distutils import bcppcompiler
import cv2
import numpy as np
import copy

win_width=1920
win_height=1080
win_name = "display"
ESC=27 # key value of ESC
square_size = 140 # size of a square (point)
x_offset = int(square_size*0.5)
y_offset = int(square_size*0.5)
delay = 1000 # display interval (ms)
b_rows = 7
b_cols = 10
# topleft topright bottomleft bottomright center
board_num = [[b_rows,b_cols],[b_rows,b_cols],[b_rows,b_cols],[b_rows,b_cols],[b_rows,b_cols]] # size of the board
board_offset = [[x_offset, y_offset], 
    [win_width-square_size*b_cols-x_offset, y_offset],
    [x_offset, win_height-square_size*b_rows-y_offset],
    [win_width-square_size*b_cols-x_offset, win_height-square_size*b_rows-y_offset],
    [int(win_width*0.5)-int(square_size*b_cols*0.5), int(win_height*0.5)-int(square_size*b_rows*0.5)]
]

# draw chessboard
def draw_cb(img, rows, cols, tl_x, tl_y):
    black_block = np.full((square_size,square_size),0)
    for row in range(rows):
        for col in range(cols):
            if (row+col)%2==0:
                row_begin = row * square_size + tl_y
                row_end = row_begin + square_size
                col_begin = col * square_size + tl_x
                col_end = col_begin + square_size
                img[row_begin:row_end,col_begin:col_end] = black_block
    # return img

if __name__=="__main__":
    cv2.namedWindow(win_name, cv2.WINDOW_NORMAL)
    cv2.setWindowProperty(win_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
    img = np.full((win_height, win_width),255, dtype=np.uint8)
    # img[:,:]=255
    key = -1
    img_copy = copy.copy(img)
    draw_cb(img_copy,7,10,board_offset[4][0],board_offset[4][1])
    cv2.imwrite("chessboard.png",img_copy)
    count = 0
    while key!=ESC:
        img_copy = copy.copy(img)
        idx = count%5
        count+=1
        rows,cols = board_num[idx]
        tl_x,tl_y = board_offset[idx]
        draw_cb(img_copy,rows,cols,tl_x,tl_y)
        cv2.imshow(win_name,img_copy)
        key = cv2.waitKey(delay)